﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Yipusoft.SoldierArchives.BLL;
using System.Drawing;

public partial class BasicBase_SoldierArchive_SoldierStat : System.Web.UI.Page
{

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            ViewState["EstablishmentSum"] = "0";
            ViewState["lblOfficeCount"] = "0";
            ViewState["lblSoldierCount"] = "0";
            ViewState["lblSoldierCount2"] = "0";
            ViewState["hlContractCount"] = "0";
            ViewState["SoldiersContract"] = "0";
            ViewState["lblNowCon"] = "0";
            BindSoldiersStat();


        }


    }
    private void BindSoldiersStat()
    {

        gvSoldierStat.DataSource = CardCode.GetCardCodes();
        gvSoldierStat.DataBind();
    }
    protected void gvSoldierStat_RowDataBound(object sender, GridViewRowEventArgs e)
    {

        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            Soldier soldier = new Soldier();
            string departmentID = gvSoldierStat.DataKeys[e.Row.RowIndex].Values[0].ToString();
            HyperLink hlSoldiersCount = e.Row.FindControl("hlSoldiersCount") as HyperLink;

            string soldierWhere = "IsDelete=0 and IsContract=0 and Departmentid like '" + departmentID.Substring(0, 4) + "%'";

            hlSoldiersCount.Text = soldier.GetSoldiersCountByWhere(soldierWhere).ToString();
            //士兵实有数


            ViewState["lblSoldierCount2"] = int.Parse(ViewState["lblSoldierCount2"].ToString()) + int.Parse(soldier.GetSoldiersCountByWhere(soldierWhere).ToString());

            hlSoldiersCount.NavigateUrl = "SoldierResult.aspx?Where=" + soldierWhere;

            HyperLink hlContractCount = e.Row.FindControl("hlContractCount") as HyperLink;



            string contractWhere = "IsDelete=0 and IsContract=1 and Departmentid like '" + departmentID.Substring(0, 4) + "%'";
            hlContractCount.Text = soldier.GetSoldiersCountByWhere(contractWhere).ToString();

            //合同制实有数
            ViewState["SoldiersContract"] = int.Parse(ViewState["SoldiersContract"].ToString()) + int.Parse(hlContractCount.Text);

            hlContractCount.NavigateUrl = "SoldierResult.aspx?Where=" + contractWhere;

            //合同制编制数
            Label lblContractEsCount = e.Row.Cells[6].FindControl("lblContractEsCount") as Label;
            ViewState["hlContractCount"] = int.Parse(ViewState["hlContractCount"].ToString()) + int.Parse(lblContractEsCount.Text);
            //干部编制数
            Label cadreEsta = e.Row.Cells[4].FindControl("cadreEsta") as Label;
            //士兵编制数
            Label SoldierEsta = e.Row.Cells[4].FindControl("SoldierEsta") as Label;

            //if (int.Parse(hlSoldiersCount.Text) > int.Parse(SoldierEsta.Text))
            //{
            //    hlSoldiersCount.ForeColor = Color.Red;
            //    hlSoldiersCount.Font.Bold = true;

            //}
            //else
            //{
            //    hlSoldiersCount.Font.Bold = false;
            //}


            Label EstablishmentSum = e.Row.FindControl("EstablishmentSum") as Label;


            EstablishmentSum.Text = Convert.ToString(Convert.ToInt32(cadreEsta.Text) + Convert.ToInt32(SoldierEsta.Text));
            //编制总数
            ViewState["EstablishmentSum"] = int.Parse(ViewState["EstablishmentSum"].ToString()) + int.Parse(EstablishmentSum.Text);

            //干部数量
            ViewState["lblOfficeCount"] = int.Parse(ViewState["lblOfficeCount"].ToString()) + Convert.ToInt32(cadreEsta.Text);

            //士兵编制数
            ViewState["lblSoldierCount"] = int.Parse(ViewState["lblSoldierCount"].ToString()) + Convert.ToInt32(SoldierEsta.Text);

            Label SoldiersContract = e.Row.FindControl("SoldiersContract") as Label;

            SoldiersContract.Text = Convert.ToString(Convert.ToInt32(hlSoldiersCount.Text) + Convert.ToInt32(hlContractCount.Text));

            //合同+士兵制数
            ViewState["lblNowCon"] = int.Parse(ViewState["lblNowCon"].ToString()) + int.Parse(SoldiersContract.Text);


            //if (int.Parse(hlContractCount.Text) > int.Parse(e.Row.Cells[7].Text))
            //{
            //    hlContractCount.ForeColor = Color.Red;
            //    hlContractCount.Font.Bold = true;

            //}
            //else
            //{
            //    hlContractCount.Font.Bold = false;
            //}

            ////lblSoldier.Text = "100";
            //最后更新时间
            Label lblSetTime = e.Row.FindControl("lblSetTime") as Label;
            lblSetTime.Text = soldier.GetLastTime(departmentID).ToString("yyyy-MM-dd HH:mm");
            //士兵缺编数
            Label SoldiersLackCount = e.Row.FindControl("SoldiersLackCount") as Label;
            int SoldiersLack = int.Parse(SoldierEsta.Text) - Convert.ToInt32(hlSoldiersCount.Text);
            SoldiersLackCount.Text = SoldiersLack.ToString();
            if (Convert.ToInt32(SoldiersLackCount.Text) < 0)
            {
                SoldiersLackCount.ForeColor = System.Drawing.Color.Red;
                SoldiersLackCount.Text = Convert.ToString(0 - SoldiersLack);
            }
            else
            {
                SoldiersLackCount.ForeColor = System.Drawing.Color.FromArgb(60, 196, 6);
            }
            //合同制缺编数
            Label ContractLackCount = e.Row.FindControl("ContractLackCount") as Label;
            int ContractLack = int.Parse(lblContractEsCount.Text) - Convert.ToInt32(hlContractCount.Text);
            ContractLackCount.Text = Convert.ToString(ContractLack);
            if (Convert.ToInt32(ContractLackCount.Text) < 0)
            {
                ContractLackCount.ForeColor = System.Drawing.Color.Red;
                ContractLackCount.Text = Convert.ToString(0 - ContractLack);
            }
            else
            {
                ContractLackCount.ForeColor = System.Drawing.Color.FromArgb(60, 196, 6);
            }
        }
        if (e.Row.RowType == DataControlRowType.Footer)
        {
            //编制总数
            (e.Row.FindControl("EstablishmentSum") as Label).Text = ViewState["EstablishmentSum"].ToString();

            //干部数量
            (e.Row.FindControl("lblOfficeCount") as Label).Text = ViewState["lblOfficeCount"].ToString();
            //士兵编制数
            (e.Row.FindControl("lblSoldierCount1") as Label).Text = ViewState["lblSoldierCount"].ToString();
            //合同制编制数
            (e.Row.FindControl("hlContractCount") as Label).Text = ViewState["hlContractCount"].ToString();

            //合同制数
            (e.Row.FindControl("SoldiersContract") as Label).Text = ViewState["SoldiersContract"].ToString();
            //士兵实有数
            (e.Row.FindControl("lblSoldierCount") as Label).Text = ViewState["lblSoldierCount2"].ToString();
            //合同制编制数
            (e.Row.FindControl("lblNowCon") as Label).Text = ViewState["lblNowCon"].ToString();

        }
    }
    protected void gvSoldierStat_DataBound(object sender, EventArgs e)
    {

    }
    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            //编制总数
            (e.Row.FindControl("EstablishmentSum") as Label).Text = ViewState["EstablishmentSum"].ToString();

            //干部数量
            (e.Row.FindControl("lblOfficeCount") as Label).Text = ViewState["lblOfficeCount"].ToString();
            //士兵编制数
            (e.Row.FindControl("lblSoldierCount1") as Label).Text = ViewState["lblSoldierCount"].ToString();
            //合同制编制数
            (e.Row.FindControl("hlContractCount") as Label).Text = ViewState["hlContractCount"].ToString();

            //合同制数
            (e.Row.FindControl("SoldiersContract") as Label).Text = ViewState["SoldiersContract"].ToString();
            //士兵实有数
            (e.Row.FindControl("lblSoldierCount") as Label).Text = ViewState["lblSoldierCount2"].ToString();
            //合同制编制数
            (e.Row.FindControl("lblNowCon") as Label).Text = ViewState["lblNowCon"].ToString();



        }
    }
}
